7.8 ArrayList 클래스
ArrayList 클래스는 IList를 구현한 대표적인 클래스입니다. 미리 IList 인터페이스를 살펴보았지만 여기서는 IList가 가지고 있는 메서드들을 테스트해 보도록 하겠습니다. ArrayList는 데이터를 삽입했을 때 순서대로 삽입되며 중간삽입이나 제거 또한 가능합니다. 물론, 여러분이 미리 살펴보았던 IList, ICollection, IEnumerable의 특징을 모두 포함하고 있습니다. 다음은 ArrayList의 프로토타입을 보여주고 있습니다.
▣ ArrayList 클래스의 프로토타입 |
□ public class ArrayList : IList, ICollection, IEnumerable, ICloneable |
다음 예에서 ArrayList 클래스의 메서드를 통해서 객체 저장, 검색, 삭제하는 것을 알아보겠습니다.
& |
SamplesArrayList.cs |
Ü ArrayList를 테스트하는 예제 |
using System; using System.Collections;
public class SamplesArrayList { public static void Main() { ArrayList myAL = new ArrayList(); myAL.Add( "hi" ); myAL.Add( "welcome" ); myAL.Add( "to" ); myAL.Add( "c#" ); myAL.Add( "world" );
Print("1. 데이터삽입:", myAL);
Queue myQueue = new Queue(); myQueue.Enqueue( "QueueData" ); myAL.AddRange(myQueue);
Print("2. Queue에 내용을 ArrayList에 삽입:", myAL);
myAL.Insert( 2,"novel");
Print("3. Index 위치 2에 데이터 삽입:", myAL);
myAL.Remove( "hi" );
Print("4. hi 데이터 삭제:", myAL);
myAL.RemoveAt( 2 );
Print("5. Index 위치 2의 데이터 삭제:", myAL);
myAL.RemoveRange(2,2);
Print("6. Index 위치 2부터 2개의 데이터 삭제:", myAL);
myAL.Sort(0, 3, null);
Print("7. Index 위치 0부터 3개 Sort:", myAL);
Console.WriteLine("8. [ myAL.Capacity ] : {0}", myAL.Capacity); Console.WriteLine("9. [ myAL.Count ] : {0}", myAL.Count); } //main
public static void Print(String info, IEnumerable myList ) { IEnumerator myEnumerator = myList.GetEnumerator(); Console.Write(info); while ( myEnumerator.MoveNext() ) Console.Write( " {0},", myEnumerator.Current ); Console.WriteLine(); } } //class |
C:\C#Example\07>csc SamplesArrayList.cs
C:\C#Example\07>SamplesArrayList
1. 데이터삽입: hi, welcome, to, c#, world,
2. Queue에 내용을 ArrayList에 삽입: hi, welcome, to, c#, world, QueueData,
3. Index 위치 2에 데이터 삽입: hi, welcome, novel, to, c#, world, QueueData,
4. hi 데이터 삭제: welcome, novel, to, c#, world, QueueData,
5. Index 위치 2의 데이터 삭제: welcome, novel, c#, world, QueueData,
6. Index 위치 2부터 2개의 데이터 삭제: welcome, novel, QueueData,
7. Index 위치 0부터 3개 Sort: novel, QueueData, welcome,
8. [ myAL.Capacity ] : 16
9. [ myAL.Count ] : 3 |
먼저, ArrayList 클래스의 객체를 하나 생성한 후 데이터를 삽입하고 있습니다. ArrayList에 데이터를 삽입할 때는 Add() 메서드를 이용합니다.
ArrayList myAL = new ArrayList();
myAL.Add( "hi" );
myAL.Add( "welcome" );
myAL.Add( "to" );
myAL.Add( "c#" );
myAL.Add( "world" );
Queue 객체에 있는 내용을 ArrayList로 삽입하는 것을 테스트하기 위해서 Queue 객체를 생성하고 Enqueue() 메서드를 이용하여 큐에 데이터를 삽입합니다.
Queue myQueue = new Queue(); //Queue 객체를 생성
myQueue.Enqueue( "QueueData" ); // Queue에 값을 삽입
Queue에 있는 모든 내용을 ArrayList의 끝부분에 삽입합니다. 이 때 사용하는 메서드는 AddRange() 메서드를 이용합니다.
myAL.AddRange( myQueue ); // ArrayList의 끝에 Queue의 모든 데이터를 삽입
ArrayList의 인덱스 2번째에 데이터를 삽입하게 됩니다. 이 때 사용하는 메서드는 Insert() 메서드 입니다. 그리고, 해당 데이터를 제거할 때는 Remove() 메서드를 사용합니다.
myAL.Insert( 2, "novel" ); Print("3. Index 위치 2에 데이터 삽입:", myAL);
myAL.Remove( "hi" ); Print("4. hi 데이터 삭제:", myAL);
ArrayList에서 위의 경우와 같이 데이터를 직접 넣어서 삽입할 수도 있으며 그리고, 인덱스(Index)로 삭제할 수도 있습니다. 이 때 사용하는 메서드는 RemoveAt() 메서드입니다. 그리고, 특정 범위 내의 모든 요소들을 삭제하고자 할 때는 RemoveRange() 메서드를 이용합니다. RemoveRange() 메서드의 첫번째 매개변수는 인덱스 번호이며 두번째 매개변수는 해당 인덱스 번호로부터 몇 개를 지울 것인지를 결정합니다.
myAL.RemoveAt(2); // 인덱스를 통해 요소를 삭제한다.
myAL.RemoveRange( 2, 2 ); // ArrayList에서 요소의 범위를 제거한다.
마지막으로 테스트하고 있는 예는 Sort() 메서드입니다. ArrayList 내의 특정한 부분을 소팅할 때 사용하는 메서드입니다. 첫번째 매개변수는 소팅할 첫 위치를 의미하며 두번째 매개변수는 소팅할 개수를 의미합니다.
myAL.Sort(0, 3, null);
마지막으로 설명드릴 것이 바로 IEnumerator입니다. ArrayList는 IEnumerable 인터페이스를 구현하여 만들어졌기 때문에 ArrayList로부터 바로 IEnumerator를 얻을 수 있습니다. IEnumerator를 이용하여 전체 ArrayList를 검색하고 있습니다.
IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( " {0},", myEnumerator.Current );
이것으로 ArrayList의 대표적인 기능들에 대하여 알아보았습니다. 다음으로 IDictionary 인터페이스의 대표적인 Hashtable 클래스에 대하여 알아보도록 하겠습니다 |